<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

class Make extends Command
{
    protected function configure()
    {
        $this->setName('make')
            ->addArgument('type', Argument::REQUIRED, 'Type: controller/model/validate')
            ->addArgument('name', Argument::REQUIRED, 'Name')
            ->setDescription('Create a new class file');
    }

    protected function execute(Input $input, Output $output)
    {
        $type = $input->getArgument('type');
        $name = $input->getArgument('name');
        
        switch($type) {
            case 'controller':
                $this->makeController($name);
                break;
            case 'model':
                $this->makeModel($name);
                break;
            case 'validate':
                $this->makeValidate($name);
                break;
            default:
                $output->writeln('Invalid type');
                return;
        }
        
        $output->writeln('Created successfully!');
    }
    
    protected function makeController($name)
    {
        $template = file_get_contents(__DIR__ . '/stubs/controller.stub');
        $content = str_replace([
            '{%className%}',
            '{%namespace%}'
        ], [
            $name,
            'app\\controller'
        ], $template);
        
        $file = app_path() . 'controller/' . $name . '.php';
        file_put_contents($file, $content);
    }
    
    protected function makeModel($name)
    {
        $template = file_get_contents(__DIR__ . '/stubs/model.stub');
        $content = str_replace([
            '{%className%}',
            '{%namespace%}'
        ], [
            $name,
            'app\\model'
        ], $template);
        
        $file = app_path() . 'model/' . $name . '.php';
        file_put_contents($file, $content);
    }
    
    protected function makeValidate($name)
    {
        $template = file_get_contents(__DIR__ . '/stubs/validate.stub');
        $content = str_replace([
            '{%className%}',
            '{%namespace%}'
        ], [
            $name,
            'app\\validate'
        ], $template);
        
        $file = app_path() . 'validate/' . $name . '.php';
        file_put_contents($file, $content);
    }
} 